package com.etms.service.impl;

import com.etms.mapper.AttendMapper;
import com.etms.pojo.Attend;
import com.etms.pojo.SysUser;
import com.etms.service.AttendService;
import com.etms.service.UserService;
import com.etms.utils.DateUtils;
import com.etms.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AttendServiceImpl implements AttendService {
    @Autowired
    AttendMapper attendMapper;
    @Autowired
    UserService userService;
    @Override
    public Result qiandao(Integer id) {
        Attend isOk=attendMapper.findByDayAndUid(Calendar.getInstance().get(Calendar.DAY_OF_YEAR),id);
        if(isOk!=null){
            return Result.failed("请勿重复签到,一天只能签到一天哦");
        }
        Attend attend=new Attend();
        attend.setUid(id);
        attend.setUpdateSalary(0.0);
        attend.setCheckTime(DateUtils.getDateTime(new Date()));//设置工作时间
        attend.setWorkNums(0);//工作量初始化为0
        attend.setDayOfYear(Calendar.getInstance().get(Calendar.DAY_OF_YEAR));

        int f=attendMapper.insert(attend);
        if(f==0)return Result.failed();
        return Result.successed("签到成功,新的一天新的开始!",null);
    }

//    @Override
//    public Result qiantui(Integer id) {
//        Attend attend=new Attend();
//        attend.setUid(id);
//        attend.setLeftTime(DateUtils.getDateTime(new Date()));//设置工作时间
//        boolean flag=attendMapper.insert(attend);
//        if(!flag){
//            return Result.failed();
//        }
//        return Result.successed("签退成功,新的一天新的开始!",null);
//    }
    @Override
    public Result getList(PageParmas pageParmas) {
        PageHelper.startPage(pageParmas.getCurrentPage(),pageParmas.getPageSize());
        List<Attend>list=attendMapper.findList();
        PageInfo pageInfo=new PageInfo(list);
        int pages = pageInfo.getPages();
        List<KaoqinVo> copyList=copyList(list);
        PageResult pageResult=new PageResult();
        pageResult.setTotalPage(pages);
        pageResult.setCurrentPage(pageParmas.getCurrentPage());
        pageResult.setList(copyList);
        return Result.successed(pageResult);
    }

    @Override
    public Result updateSarely(AttendParmas attendParmas) {
        boolean f=attendMapper.updateSaraly(attendParmas);
        if(!f){
            return Result.failed();
        }
        String msg="";
        if (attendParmas.getUpdateSalary()>0)msg="加工资成功!将在今天结束之后生效";
        else msg="扣工资成!将在今天结束之后生效";
        return Result.successed(msg,null);
    }

    @Override
    public Result getPaihang() {//查找今日排行前五工作量
        List<Attend> attendList=attendMapper.findByDayAndWorks(Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
        List<KaoqinVo>kaoqinVoList=copyList(attendList);
        return Result.successed(kaoqinVoList);
    }


    private List<KaoqinVo> copyList(List<Attend> list) {
        List<KaoqinVo> copyList=new ArrayList<>();
        for (Attend attend : list) {
            KaoqinVo vo=copy(attend);
            copyList.add(vo);
        }
        return copyList;
    }

    private KaoqinVo copy(Attend attend) {
        KaoqinVo kaoqinVo=new KaoqinVo();
        kaoqinVo.setAttend(attend);
        Integer uid = attend.getUid();
        Result byId = userService.findById(uid);
        SysUser user = (SysUser) byId.getData();
        kaoqinVo.setSysUser(user);
        return kaoqinVo;
    }
}
